using System;
using System.Web;
using System.Web.UI;
namespace HKB.Common
{
	public class Cookie : Page
	{
		public static void SaveCookie(string CookieName, string CookieValue, int CookieTime, string CookieDomainName)
		{
			Cookie.SaveCookie(CookieName, CookieValue, CookieTime, CookieDomainName, true);
		}
		public static void SaveCookie(string CookieName, string CookieValue, int CookieTime, string CookieDomainName, bool Endcode)
		{
			HttpCookie httpCookie = new HttpCookie(CookieName);
			if (Endcode)
			{
				httpCookie.Value = HttpContext.Current.Server.UrlEncode(CookieValue);
			}
			else
			{
				httpCookie.Value = CookieValue;
			}
			if (CookieDomainName != "")
			{
				httpCookie.Domain = CookieDomainName;
			}
			System.TimeSpan value = new System.TimeSpan(0, 0, 0, 0);
			System.DateTime now = System.DateTime.Now;
			switch (CookieTime)
			{
			case 1:
				value = new System.TimeSpan(1, 0, 0, 0);
				break;
			case 2:
				value = new System.TimeSpan(30, 0, 0, 0);
				break;
			case 3:
				value = new System.TimeSpan(365, 0, 0, 0);
				break;
			default:
				if (CookieTime > 3)
				{
					value = new System.TimeSpan(0, CookieTime, 0);
				}
				break;
			}
			if (CookieTime != 0)
			{
				httpCookie.Expires = now.Add(value);
			}
			if (HttpContext.Current.Response.Cookies[CookieName] != null)
			{
				HttpContext.Current.Response.Cookies.Remove(CookieName);
			}
			HttpContext.Current.Response.Cookies.Add(httpCookie);
		}
		public static void SaveCookie(string CookieName, string CookiesName, string CookieValue, int CookieTime, string CookieDomainName)
		{
			HttpCookie httpCookie;
			if (HttpContext.Current.Request[CookieName] != null)
			{
				httpCookie = HttpContext.Current.Request.Cookies[CookieName];
				if (httpCookie.Values[CookiesName] != null && httpCookie.Values[CookiesName] != "")
				{
					httpCookie.Values.Remove(CookiesName);
				}
			}
			else
			{
				httpCookie = new HttpCookie(CookieName);
			}
			if (CookieDomainName != "")
			{
				httpCookie.Domain = CookieDomainName;
			}
			System.TimeSpan value = new System.TimeSpan(0, 0, 0, 0);
			System.DateTime now = System.DateTime.Now;
			switch (CookieTime)
			{
			case 1:
				value = new System.TimeSpan(1, 0, 0, 0);
				break;
			case 2:
				value = new System.TimeSpan(30, 0, 0, 0);
				break;
			case 3:
				value = new System.TimeSpan(365, 0, 0, 0);
				break;
			default:
				if (CookieTime > 3)
				{
					value = new System.TimeSpan(0, CookieTime, 0);
				}
				break;
			}
			if (CookieTime != 0)
			{
				httpCookie.Expires = now.Add(value);
			}
			httpCookie.Values.Add(CookiesName, HttpContext.Current.Server.UrlEncode(CookieValue));
			HttpContext.Current.Response.Cookies.Add(httpCookie);
		}
		public static void ClearCookie(string CookieName, string CookieDomainName)
		{
			HttpCookie httpCookie = new HttpCookie(CookieName);
			System.DateTime now = System.DateTime.Now;
			if (CookieDomainName != "")
			{
				httpCookie.Domain = CookieDomainName;
			}
			httpCookie.Value = "";
			httpCookie.Expires = now.AddYears(-2);
			HttpContext.Current.Response.Cookies.Add(httpCookie);
		}
		public static string GetCookie(string CookieName)
		{
			HttpCookie httpCookie = new HttpCookie(CookieName);
			httpCookie = HttpContext.Current.Request.Cookies[CookieName];
			string result;
			if (httpCookie != null)
			{
				result = HttpContext.Current.Server.UrlDecode(httpCookie.Value);
			}
			else
			{
				result = "";
			}
			return result;
		}
		public static string GetCookie(string CookieName, string CookiesName)
		{
			HttpCookie httpCookie = new HttpCookie(CookieName);
			httpCookie = HttpContext.Current.Request.Cookies[CookieName];
			string result;
			if (httpCookie != null)
			{
				result = HttpContext.Current.Server.UrlDecode(httpCookie.Values[CookiesName]);
			}
			else
			{
				result = "";
			}
			return result;
		}
	}
}
